﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using NewsGator.Social.Data;
using NewsGator.Social;
using System.Collections.Generic;
using System.Web;

namespace NGPT.ApproveIt
{
    /// <summary>
    /// Application page code-behing class. Implements IApproveItView interface.
    /// </summary>
    public partial class ApproveIt : LayoutsPageBase, IApproveItView
    {
        private ApproveItPresenter presenter;

        /// <summary>
        /// Button click event handler.
        /// </summary>
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (presenter == null)
            {
                presenter = new ApproveItPresenter(this, new ApprovalService());
            }

            presenter.RequestApproval();
        }

        /// <summary>
        /// Provides approver's login name.
        /// </summary>
        public string ApproverLoginName
        {
            get
            {
                if (userPicker.Entities.Count > 0)
                {
                    PickerEntity pe = (PickerEntity)userPicker.Entities[0];
                    if (pe.IsResolved)
                        return pe.Description;
                    else
                        throw new ArgumentException("Approver not resolved.");
                }
                else
                {
                    throw new ArgumentException("Approver not specified.");
                }
            }
            set {}
        }

        /// <summary>
        /// Provides approver's login name.
        /// </summary>
        public string RequestText
        {
            get
            {
                return tbRequestText.Text;    
            }
        }

        /// <summary>
        /// Set error message text.
        /// </summary>
        public string ErrorMessage
        {
            set 
            { 
                lErrorMessage.Text = value;
                lErrorMessage.Visible = !string.IsNullOrEmpty(lErrorMessage.Text);
            }
        }

        /// <summary>
        /// Set submit result message.
        /// </summary>
        public string ResultMessage
        {
            get { return null; }
            set
            {
                lResultMessage.Text = value;
                lResultMessage.Visible = !string.IsNullOrEmpty(lResultMessage.Text);
            }
        }

        /// <summary>
        /// Provides site URL from the page request.
        /// </summary>
        public string SiteUrl
        {
            get
            {
                return Request["SiteUrl"];
            }
        }

        /// <summary>
        /// Provides list Id from the page request.
        /// </summary>
        public Guid ListId
        {
            get 
            {
                return new Guid(Request["ListId"]);
            }
        }

        /// <summary>
        /// Provides item Id from the page request.
        /// </summary>
        public int ItemId
        {
            get
            {
                return Convert.ToInt32(Request["ItemId"]);
            }
        }
    }
}
